package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SysParamMapper;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 系统参数表操作接口实现
 * Created by 王成 on 2018/7/3.
 */
public class SysParamMapperImpl implements SysParamMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public SysParamMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public void loadSysParamInfo(Set<String> dbPoolCashNode, List<SysParam> sysParamList) throws Exception {
        for (String dbCaseNodeName : dbPoolCashNode) {
            String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbCaseNodeName, AppParamConstant.OLTP_POOL_NAME);
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "loadSysParamInfo");
            List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, null, SysParam.class);
            if (objectList.isEmpty()) {
                rglog.warn("获取数据库连接池【{}】全量系统参数信息失败!", dbPoolName);
            } else {
                sysParamList.addAll(objectList.stream().map(item -> (SysParam) (item)).collect(Collectors.toList()));
            }
        }
    }

    @Override
    public SysParam selectSysParamByPrimaryKey(String instId, String paramType, String paramKey) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSysParamByPrimaryKey");
        SysParam sysParam = new SysParam();
        sysParam.setInstId(instId);
        sysParam.setParamType(paramType);
        sysParam.setParamKey(paramKey);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, sysParam, sysParam);
        if (0 != iReturnCode) {
            rglog.warn("查询系统参数表失败,instId:<{}>,paramType:<{}>,paramKey:<{}>,iReturnCode:<{}>", instId, paramType, paramKey, iReturnCode);
            return null;
        }
        return sysParam;
    }

    @Override
    public List<SysParam> selectSysParamByInstIdAndParamType(String instId, String paramType) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSysParamByInstIdAndParamType");
        SysParam sysParam = new SysParam();
        sysParam.setInstId(instId);
        sysParam.setParamType(paramType);
        List<Object> objectList = dbsUtil.dbsFetchListData(sqlStr, sysParam, SysParam.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据机构标识<{}>及参数类型<{}>查询参数信息失败!", instId, paramType);
            return null;
        }
        return objectList.stream().map(item -> (SysParam) item).collect(Collectors.toList());
    }

    @Override
    public List<SysParam> selectSysParamByInstId(String instId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSysParamByInstId");
        SysParam sysParam = new SysParam();
        sysParam.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, sysParam, SysParam.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据机构标识<{}>查询参数信息失败!", instId);
            return null;
        }
        return objectList.stream().map(item -> (SysParam) item).collect(Collectors.toList());
    }

    @Override
    public int insertSysParam(SysParam sysParam) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSysParam");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, sysParam, null);
        if (0 != iReturnCode) {
            rglog.error("登记系统参数表失败,instId:<{}>,paramType:<{}>,paramKey:<{}>,iReturnCode:<{}>", sysParam.getInstId(), sysParam.getParamType(), sysParam.getParamKey(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateSysParamByPrimaryKey(SysParam sysParam) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSysParamByPrimaryKey");
        sysParam.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, sysParam, null);
        if (0 != iReturnCode) {
            rglog.error("更新系统参数表失败,instId:<{}>,paramType:<{}>,paramKey:<{}>,iReturnCode:<{}>", sysParam.getInstId(), sysParam.getParamType(), sysParam.getParamKey(), iReturnCode);
        }
        return iReturnCode;
    }
}
